﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using YJKCodeBase.WinForm;

namespace ModelChecking.ModelValidation.FireCompartment
{
	public partial class DlgSelectFireDefAreaView : System.Windows.Forms.Form
	{
		public bool IsDlgValid { get; set; }

		public DlgSelectFireDefAreaView()
		{
			this.IsDlgValid = true;
			this.InitializeComponent();
		}

		public DlgSelectFireDefAreaView(ExternalCommandData cmdData)
		{
			this.IsDlgValid = true;
			this.InitializeComponent();
			Process currentProcess = Process.GetCurrentProcess();
			IntPtr mainWindowHandle = currentProcess.MainWindowHandle;
			this._hWndSelectFloorForm = new WindowHandle(mainWindowHandle);
			this.m_Revit = cmdData;
			this.GetAllFireDefAreaViews();
			foreach (ViewPlan viewPlan in this.m_sortedViewPlanList)
			{
				int index = this.clbView.Items.Add(viewPlan.Name);
				this.clbView.SetItemChecked(index, true);
			}
			if (this.m_sortedViewPlanList.Count == 0)
			{
				YJKMessageBox.Show("当前项目中没有防火分区面积平面，请检查！");
				this.IsDlgValid = false;
			}
		}

		private void GetAllFireDefAreaViews()
		{
			List<Level> sortLevels = this.GetSortLevels(this.m_Revit.Application.ActiveUIDocument.Document, double.MinValue, double.MaxValue);
			List<ViewPlan> list = new List<ViewPlan>();
			List<double> list2 = new List<double>();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_Revit.Application.ActiveUIDocument.Document).OfClass(typeof(ViewPlan));
			foreach (Element element in filteredElementCollector)
			{
				ViewPlan viewPlan = element as ViewPlan;
                if (viewPlan != null && (int)viewPlan.ViewType == 116 && viewPlan.GenLevel != null && viewPlan.AreaScheme.Name.Contains("防火"))
				{
					list2.Add(viewPlan.GenLevel.Elevation);
					list.Add(viewPlan);
				}
			}
			for (int i = 0; i < sortLevels.Count; i++)
			{
				for (int j = 0; j < list2.Count; j++)
				{
					if (this.IsEqual(sortLevels[i].Elevation, list2[j]))
					{
						this.m_sortedViewPlanList.Add(list[j]);
					}
				}
			}
		}

		public List<Level> GetSortLevels(Autodesk.Revit.DB.Document doc, double bottom, double top)
		{
			List<Level> list = new List<Level>();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			ICollection<Element> collection = filteredElementCollector.OfClass(typeof(Level)).ToElements();
			foreach (Element element in collection)
			{
				Level level = element as Level;
				double elevation = level.Elevation;
				if (elevation >= bottom && elevation <= top)
				{
					list.Add(level);
				}
			}
			list.Sort(new DlgSelectFireDefAreaView.LevelComparer());
			return list;
		}

		private bool LessThan(double val1, double val2)
		{
			return val1 - val2 < -1E-09;
		}

		private bool IsEqual(double val1, double val2)
		{
			return !this.LessThan(val1, val2) && !this.LessThan(val2, val1);
		}

		private void btnSelAll_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.clbView.Items.Count; i++)
			{
				this.clbView.SetItemChecked(i, true);
			}
		}

		private void btnSelNone_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.clbView.Items.Count; i++)
			{
				this.clbView.SetItemChecked(i, false);
			}
		}

		private void btnSelOb_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.clbView.Items.Count; i++)
			{
				bool value = !this.clbView.GetItemChecked(i);
				this.clbView.SetItemChecked(i, value);
			}
		}

		public List<ViewPlan> SelectedViews()
		{
			List<ViewPlan> list = new List<ViewPlan>();
			for (int i = 0; i < this.clbView.Items.Count; i++)
			{
				bool itemChecked = this.clbView.GetItemChecked(i);
				if (itemChecked)
				{
					list.Add(this.m_sortedViewPlanList.ElementAt(i));
				}
			}
			return list;
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
		}

		private void DlgSelectFireDefAreaView_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
		}

		private const double _epsDouble = 1E-09;

        public WindowHandle _hWndSelectFloorForm;

		private ExternalCommandData m_Revit;

		private List<ViewPlan> m_sortedViewPlanList = new List<ViewPlan>();

		private class LevelComparer : IComparer<Level>
		{
			public int Compare(Level x, Level y)
			{
				return x.Elevation.CompareTo(y.Elevation);
			}
		}
	}
}
